﻿$(function () {

    //Add save icon in button with class 'saveBTN'
     $(".saveBTN").button({
            icons: {
                primary: "ui-icon-disk"
            }
        });

    //Add close icon in button with class 'delBTN'
        $(".delBTN").button({
            icons: {
                primary: "ui-icon-circle-close"
            }
        });

        //Add search icon in button with class 'searchBTN'
        $(".searchBTN").button({
            icons: {
                primary: "ui-icon-search"
            }
        });


    //Hide field for datepicker
    $('#datetime_checkbox').change(function () {
        $('#datepicker').parent().toggle(this.checked);
    });

    // Accordion
    $("#accordion").accordion({ header: "h3" });

    // Tabs
    $('#tabs').tabs();


    // Dialog			
    $('#dialog').dialog({
        autoOpen: false,
        width: 600,
        buttons: {
            "Ok": function () {
                $(this).dialog("close");
            },
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });

    // Dialog Link
    $('#dialog_link').click(function () {
        $('#dialog').dialog('open');
        return false;
    });

    // Datepicker
    $('#datepicker').datepicker({
        dateFormat: 'dd-mm-yy',
        inline: true
    });

    // Slider
    $('#slider').slider({
        range: true,
        values: [17, 67]
    });

    // Progressbar
    $("#progressbar").progressbar({
        value: 20
    });

    //hover states on the static widgets
    $('#dialog_link, ul#icons li').hover(
					function () { $(this).addClass('ui-state-hover'); },
					function () { $(this).removeClass('ui-state-hover'); }
				);





    // portlets   
    $(".column").sortable({
        connectWith: ".column"
    });

    $(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
			.find(".portlet-header")
				.addClass("ui-widget-header ui-corner-all")
				.prepend("<span class='ui-icon ui-icon-minusthick'></span>")
				.end()
			.find(".portlet-content");

    $(".portlet-header .ui-icon").click(function () {
        $(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
        $(this).parents(".portlet:first").find(".portlet-content").toggle();
    });

    $(".column").disableSelection();

    // portlets voor starting in minus mode
    $(".portlet-min").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
			.find(".portlet-header-min")
				.addClass("ui-widget-header ui-corner-all")
				.prepend("<span class='ui-icon ui-icon-plusthick'></span>")
				.end()

			.find(".portlet-content-min").toggle();

    $(".portlet-header-min .ui-icon").click(function () {
        $(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
        $(this).parents(".portlet-min:first").find(".portlet-content-min").toggle();
    });

    // Geeft een input button deze stijl.
    $("button, input:submit").button();






    // Functies voor pickme table
    function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function () {
                oldonload();
                func();
            }
        }
    }

    function addClass(element, value) {
        if (!element.className) {
            element.className = value;
        } else {
            newClassName = element.className;
            newClassName += " ";
            newClassName += value;
            element.className = newClassName;
        }
    }


    function stripeTables() {
        var tables = document.getElementsByTagName("table");
        for (var m = 0; m < tables.length; m++) {
            if (tables[m].className == "pickme") {
                var tbodies = tables[m].getElementsByTagName("tbody");
                for (var i = 0; i < tbodies.length; i++) {
                    var odd = true;
                    var rows = tbodies[i].getElementsByTagName("tr");
                    for (var j = 0; j < rows.length; j++) {
                        if (odd == false) {
                            odd = true;
                        } else {
                            addClass(rows[j], "odd");
                            odd = false;
                        }
                    }
                }
            }
        }
    }
    function highlightRows() {
        if (!document.getElementsByTagName) return false;
        var tables = document.getElementsByTagName("table");
        for (var m = 0; m < tables.length; m++) {
            if (tables[m].className == "pickme") {
                var tbodies = tables[m].getElementsByTagName("tbody");
                for (var j = 0; j < tbodies.length; j++) {
                    var rows = tbodies[j].getElementsByTagName("tr");
                    for (var i = 0; i < rows.length; i++) {
                        rows[i].oldClassName = rows[i].className
                        rows[i].onmouseover = function () {
                            if (this.className.indexOf("selected") == -1)
                                addClass(this, "highlight");
                        }
                        rows[i].onmouseout = function () {
                            if (this.className.indexOf("selected") == -1)
                                this.className = this.oldClassName
                        }
                    }
                }
            }
        }
    }

    function selectRowCheckbox(row) {
        var checkbox = row.getElementsByTagName("input")[0];
        if (checkbox.checked == true) {
            checkbox.checked = false;
        } else
            if (checkbox.checked == false) {
                checkbox.checked = true;
            }
    }

    function lockRow() {
        var tables = document.getElementsByTagName("table");
        for (var m = 0; m < tables.length; m++) {
            if (tables[m].className == "pickme") {
                var tbodies = tables[m].getElementsByTagName("tbody");
                for (var j = 0; j < tbodies.length; j++) {
                    var rows = tbodies[j].getElementsByTagName("tr");
                    for (var i = 0; i < rows.length; i++) {
                        rows[i].oldClassName = rows[i].className;
                        rows[i].onclick = function () {
                            if (this.className.indexOf("selected") != -1) {
                                this.className = this.oldClassName;
                            } else {
                                addClass(this, "selected");
                            }
                            selectRowCheckbox(this);
                        }
                    }
                }
            }
        }
    }

    addLoadEvent(stripeTables);
    addLoadEvent(highlightRows);
    addLoadEvent(lockRow);


    function lockRowUsingCheckbox() {
        var tables = document.getElementsByTagName("table");
        for (var m = 0; m < tables.length; m++) {
            if (tables[m].className == "pickme") {
                var tbodies = tables[m].getElementsByTagName("tbody");
                for (var j = 0; j < tbodies.length; j++) {
                    var checkboxes = tbodies[j].getElementsByTagName("input");
                    for (var i = 0; i < checkboxes.length; i++) {
                        checkboxes[i].onclick = function (evt) {
                            if (this.parentNode.parentNode.className.indexOf("selected") != -1) {
                                this.parentNode.parentNode.className = this.parentNode.parentNode.oldClassName;
                            } else {
                                addClass(this.parentNode.parentNode, "selected");
                            }
                            if (window.event && !window.event.cancelBubble) {
                                window.event.cancelBubble = "true";
                            } else {
                                evt.stopPropagation();
                            }
                        }
                    }
                }
            }
        }
    }
    addLoadEvent(lockRowUsingCheckbox);
    // Einde functies pickme tables


    $('input[type=text][title],input[type=password][title],textarea[title]').each(function (i) {
        $(this).addClass('italic-input-prompt-' + i);
        var promptSpan = $('<span class="italic-input-prompt"/>');
        $(promptSpan).attr('id', 'italic-input-prompt-' + i);
        $(promptSpan).append($(this).attr('title'));
        $(promptSpan).click(function () {
            $(this).hide();
            $('.' + $(this).attr('id')).focus();
        });
        if ($(this).val() != '') {
            $(promptSpan).hide();
        }
        $(this).before(promptSpan);
        $(this).focus(function () {
            $('#italic-input-prompt-' + i).hide();
        });
        $(this).blur(function () {
            if ($(this).val() == '') {
                $('#italic-input-prompt-' + i).show();
            }
        });
    });
});

function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}